package com.fone.player.online.adapter;

import java.util.List;

import com.fone.player.R;
import com.fone.player.entity.SoftInfo;
import com.fone.player.util.FoneUtil;
import com.fone.player.util.ScreenUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SoftAdapter extends BaseAdapter {
	/** 日志标签 */
//	private static final String TAG = "SoftAdapter";
	
	private String shost;
//	private ArrayList<Cnt> softList;
	private List<SoftInfo> softList;
	
	private LayoutInflater layoutInflater;
	
	DisplayImageOptions options;
	
	public SoftAdapter(Context context, String shost, List<SoftInfo> softList) {
		super();
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.default_image_soft)
		.showImageForEmptyUri(R.drawable.default_image_soft)
		.showImageOnFail(R.drawable.default_image_soft)
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.considerExifParams(true)
		.displayer(new RoundedBitmapDisplayer(ScreenUtil.dp2px(15)))
		.build();
		
		initData(shost, softList);
		layoutInflater = LayoutInflater.from(context);
	}

	public void initData(String shost, List<SoftInfo> softList) {
		this.shost = shost;
		this.softList = softList;
	}

	@Override
	public int getCount() {
		if(softList!=null){
			return softList.size();
		}else{
			return 0;
		}
		
	}

	@Override
	public Object getItem(int position) {
		return softList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		SoftInfo info = softList.get(position);
		
		ViewHolder vHolder = null;
		if(convertView == null){
			convertView = layoutInflater.inflate(R.layout.item_soft, null);
			// 创建  ViewHodler 对象
			vHolder = new ViewHolder();
			vHolder.pic = (ImageView)convertView.findViewById(R.soft.pic);
			vHolder.title = (TextView)convertView.findViewById(R.soft.name);
			// 设置 Tag
            convertView.setTag(vHolder);
		}else{
			vHolder = (ViewHolder) convertView.getTag();
		}
		
		// 设置图片
			//ImageDownloadModule.getInstance().download(FoneUtil.getAbsoluteUrl("", shost, info.pic1), R.drawable.default_184, vHolder.pic);
		ImageLoader.getInstance().displayImage(FoneUtil.getAbsoluteUrl("", shost, info.pic1), vHolder.pic, options);
//			L.v(TAG, "getView", url);
		
        vHolder.title.setText(info.name);
        return convertView;
	}
	
	static class ViewHolder {
		ImageView pic;
        TextView title;
    }
}
